import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_getx/app/modules/message/component/text_message.dart';
import 'package:get/get_state_manager/src/simple/get_view.dart';
import '../../../const.dart';
import '../../../data/model/chat_message.dart';
import '../controllers/message_controller.dart';
import 'img_message.dart';


class Message extends GetView<MessageController> {
   Message({
    Key? key,
    required this.message
  }) : super(key: key);

  final ChatMessage message;

  Widget messageContain(ChatMessage message) {
    switch (message.contentType) {
      case ChatContentType.text:
        return TextMessage(message: message);
      case ChatContentType.image:
        return ImgMessage(message: message);
      default:
        return const SizedBox();
    }
  }
  @override
  Widget build(BuildContext context) {

    return Padding(
      padding: const EdgeInsets.only(top: kDefaultPadding),
      child: Row(
        mainAxisAlignment: message.isSender ? MainAxisAlignment.end : MainAxisAlignment.start,
        children: [
          if (!message.isSender) ...[
              GestureDetector(
                onTap: () {
                  this.controller.toProfile(this.message.fromUserId);
                },
                child: controller.userInfoService.avatarMap[this.message.fromUserId]! == "" ? Icon(Icons.account_circle_rounded, size: 45, color: Colors.blue,):
                CircleAvatar(
                  radius: 12,
                  backgroundImage: NetworkImage(controller.userInfoService.avatarMap[this.message.fromUserId]!),
                ),
              )
            ,
            const SizedBox(
              width: kDefaultPadding / 2,
            )
          ],
          if (!message.isSender)
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(controller.userInfoService.nicknameMap[this.message.fromUserId]!, style: TextStyle(fontSize: 9),),
                messageContain(message)
              ],
            )
          else messageContain(message),
          if (message.isSender) MessageStatusDot(status: message.messageStatus)
        ],
      ),
    );
  }
}

class MessageStatusDot extends StatelessWidget {
  final MessageStatus? status;

  const MessageStatusDot({Key? key, this.status}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    Color dotColor(MessageStatus status) {
      switch (status) {
        case MessageStatus.not_sent:
          return kErrorColor;
        case MessageStatus.not_view:
          return Theme.of(context).textTheme.bodyText1!.color!.withOpacity(0.1);
        case MessageStatus.viewed:
          return kPrimaryColor;
        default:
          return Colors.transparent;
      }
    }

    return Container(
      margin: const EdgeInsets.only(left: kDefaultPadding / 2),
      height: 15,
      width: 12,
      decoration: BoxDecoration(
        color: dotColor(status!),
        shape: BoxShape.circle,
      ),
      child: Icon(
        status == MessageStatus.not_sent ? Icons.close : Icons.done,
        size: 11,
        color: Theme.of(context).scaffoldBackgroundColor,
      ),
    );
  }
}
